package com.ruan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruan.entity.AttrKey;
import com.ruan.entity.Category;
import com.ruan.entity.R;
import com.ruan.service.AttrKeyService;
import com.ruan.mapper.AttrKeyMapper;
import com.ruan.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.w3c.dom.Attr;

import java.util.List;

/**
* @author 59499
* @description 针对表【attr_key】的数据库操作Service实现
* @createDate 2023-09-08 23:51:41
*/
@Service
public class AttrKeyServiceImpl extends ServiceImpl<AttrKeyMapper, AttrKey>
    implements AttrKeyService{

    @Autowired
    private AttrKeyMapper attrKeyMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public R<Void> addAttributeKey(AttrKey attrKey) {
        QueryWrapper<AttrKey> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",attrKey.getCategoryId());
        wrapper.eq("title",attrKey.getTitle());
        wrapper.eq("status",1);
        if (attrKeyMapper.selectOne(wrapper) != null){
            return new R<>(100,"该属性已存在");
        }
        int row = attrKeyMapper.insert(attrKey);
        if (row != 1){
            return new R<>(100,"添加失败");
        }
        return new R<>(200,"添加成功");
    }

    @Override
    public R<List<AttrKey>> getAttributeKey(Integer categoryId) {
        QueryWrapper<AttrKey> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",categoryId);
        wrapper.eq("status",1);
        List<AttrKey> attrKeyList1 = attrKeyMapper.selectList(wrapper);
        return new R<>(200,"查询成功",attrKeyList1);
    }

    @Override
    @Transactional
    public R<Void> deleteAttributeKeyById(Integer id) {
        AttrKey attrKey = new AttrKey();
        attrKey.setId(id);
        attrKey.setStatus(0);
        int row = attrKeyMapper.updateById(attrKey);
        if (row != 1){
            return new R<>(100,"删除失败");
        }
        return new R<>(200,"删除成功");
    }

    @Override
    public R<String[]> getAttrKeyOptionsById(Integer id) {
        AttrKey attrKey = attrKeyMapper.selectById(id);
        String[] options = attrKey.getOptions().split(",");
        return new R<>(200,"查询成功",options);
    }

    @Override
    public R<Void> deleteAttrKeyOptionByIndex(Integer categoryId, Integer attrKeyId, Integer index) {
        QueryWrapper<AttrKey> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",categoryId);
        wrapper.eq("id",attrKeyId);
        AttrKey attrKey = attrKeyMapper.selectOne(wrapper);
        String[] options = attrKey.getOptions().split(",");
        String[] newOptions = new String[options.length-1];
        // 使用循环将除指定索引外的其他字符复制到新的数组中
        for (int i = 0, j = 0; i < options.length; i++) {
            if (i != index) {
                newOptions[j++] = options[i];
            }
        }
        String result = String.join(",", newOptions);
        attrKey.setOptions(result);
        int row = attrKeyMapper.updateById(attrKey);
        if (row != 1){
            return new R<>(100,"删除失败");
        }
        return new R<>(200,"删除成功");
    }
}




